﻿using Autofac;
using Autofac.Core;
using Autofac.Extensions.DependencyInjection;
using WitsWebApi.Application.Services;
using WitsWebApi.DataContext.DapperHelper;
using WitsWebApi.DataContext.DapperRepositories;
using WitsWebApi.DataContext.Repositories;
using WitsWebApi.DataContext.RepositoryBase;

namespace WitsWebApi.AutoFac
{
    public static class IAutofacHostBuilderExtensions
    {
        public static IHostBuilder UseAutofac(this IHostBuilder hostBuilder)
        {
            return hostBuilder.UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureContainer<ContainerBuilder>(containerBuilder =>
                {
                    containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
                    containerBuilder.RegisterType<DapperContext>().InstancePerLifetimeScope();

                    #region 注册基类
                    // EF 注册
                    containerBuilder.RegisterType<AdminRepository>().As<IAdminRepository>().InstancePerLifetimeScope();
                    containerBuilder.RegisterType<AdminDetailService>().As<IAdminDetailService>().InstancePerLifetimeScope();
                    // Dapper 注册
                    containerBuilder.RegisterType<MachineRepository>().As<IMachineRepository>().InstancePerLifetimeScope();
                    #endregion
                });
        }
    }

}
